Create/Retrieve/Update/Delete is needed for many tables.
Schema.yml
propel:
blog_article:
id: ~
title: varchar(255)
content: longvarchar
author: integer
category: integer
is_published: integer
created_at: ~
blog_comment:
id: ~
blog_article_id: ~
author: varchar(255)
content: longvarchar
created_at: ~
building model
symfony propel:build-all
symfony propel:build-filters
symfony propel:build-forms
httpd-vhosts.conf
<Directory "c:/wamp/bin/php/php5.2.6/data/symfony">
AllowOverride All
Options None
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/mohammad/www/ch14/web"
Alias /sf c:/wamp/bin/php/php5.2.6/data/symfony/web/sf
ServerName c.localhost
ServerAlias www.c.localhost
</VirtualHost>
Initiating...
php symfony propel:generate-admin frontend BlogArticle --module=article
symfony plugin:publish-assets
Result
// Actions in actions/actions.class.php
index // Displays the list of the records of the table
filter // Updates the filters used by the list
new // Displays the form to create a new record
create // Creates a new record
edit // Displays a form to modify the fields of a record
update // Updates an existing record
delete // Deletes a record
batch // Executes an action on a list of selected records
// In templates/
_assets.php
_filters.php
_filters_field.php
_flashes.php
_form.php
_form_actions.php
_form_field.php
_form_fieldset.php
_form_footer.php
_form_header.php
_list.php
_list_actions.php
_list_batch_actions.php
_list_field_boolean.php
_list_footer.php
_list_header.php
_list_td_actions.php
_list_td_batch_actions.php
_list_td_stacked.php
_list_td_tabular.php
_list_th_stacked.php
_list_th_tabular.php
_pagination.php
editSuccess.php
indexSuccess.php
newSuccess.php
symfony propel:generate-admin backend BlogComment --module=comment
<?php
class BlogArticle extends BaseBlogArticle
{
public function __toString(){
return $this->getTitle();
}
}
Customizing
generator
:
class: sfPropelGenerator
param
:
model_class
: BlogArticle
theme
: admin
non_verbose_templates
: true
with_show
: false
singular
: ~
plural
: ~
route_prefix
: blog_article
with_propel_route
: 1
config
:
actions
: ~
fields
: ~
list: ~
filter
: ~
form
: ~
edit
: ~
new: ~
Customized...
generator
:
class: sfPropelGenerator
param
:
model_class
: BlogArticle
theme
: admin
non_verbose_templates
: true
with_show
: false
singular
: ~
plural
: ~
route_prefix
: article
with_propel_route
: 1
config
:
actions
:
_new
: { label
: "Create a new article", credentials
: editor
}
fields
:
author_id
: { label
: Article author
}
published_on
: { credentials
: editor
}
list:
title
: Articles
display
: [title
, author_id
, category_id
]
fields
:
published_on
: { date_format
: dd
/MM
/yy
}
layout
: stacked
params
: |
%%is_published
%%<strong
>%%=title
%%</strong
><br
/><em
>by
%%author
%%
in
%%category
%% (%%published_on
%%)</em
><p
>%%content_summary
%%</p
>
max_per_page
: 2
sort: [title
, asc
]
filter
:
display
: [title
, category_id
, author_id
, is_published
]
form
:
display
:
"Post": [title
, category_id
, content
]
"Workflow": [author_id
, is_published
, created_at
]
fields
:
published_at
: { help
: "Date of publication" }
title
: { attributes
: { style
: "width: 350px" } }
new:
title
: New article
edit
:
title
: Editing article
"%%title%%"
config:
fields:
title:
label: Article Title
attributes: { class: foo }
content: { label: Body, help: Fill in the article body }
Specific Field Setting
config
:
fields
:
title
: { label
: Article Title
}
content
: { label
: Body
}
list:
fields
:
title
: { label
: Title
}
form
:
fields
:
content
: { label
: Body of the article
}
How many columns to show?
config
:
fields
:
article_id
: { label
: Article
}
created_at
: { label
: Published on
}
content
: { label
: Body
}
list:
display
: [id
, article_id
, content
]
form
:
display
:
NONE
: [article_id
]
Editable
: [author
, content
, created_at
]
Custom Fields
config
:
list:
display
: [id
, title
, nb_comments
, created_at
]
/lib/Model/BlogArticle.php
public function getNbComments()
{
return $this->countBlogComments();
}
/code>
Even HTML output:
<code:php>
public function getArticleLink()
{
return link_to($this->getBlogArticle()->getTitle(), 'article_edit', $this->getBlogArticle());
}
Partial Fields
config
:
list:
display
: [id
, _article_link
, created_at
]
_article_link.php
<?php echo link_to
($comment->getBlogArticle()->getTitle(), '@article_edit', $comment->getBlogArticle()) ?>
Every partial has access to a variable named by the class.
Two partials?
We have _article_link for both edit/new and list but with different meanings.
$type is used to distinguish the two.
Component
config:
fields:
article_link: { label: Article }
Looks for articleLink component of the current module.
View Title
config
:
list:
title
: List of Articles
new:
title
: New Article
edit
:
title
: Edit Article
%%title
%% (%%id
%%) Tooltips
config
:
edit
:
fields
:
article_id
: { help
: The
current comment relates to this article
}
Date Format
config
:
list:
fields
:
created_at
: { label
: Published
, date_format
: dd
/MM
}
I18N
<p>
<?php echo __
('No result', array(), 'sf_admin') ?></p>
Moving the HyperLink
config
:
list:
display
: [article_link
, =content
]
Layout
config
:
list:
layout
: stacked
params
: |
%%=content
%%<br
/>
(sent by
%%author
%% on
%%created_at
%% about
%%article_link
%%)
display
: [created_at
, author
, content
]
Sorting
config
:
list:
sort: created_at
# Alternative syntax, to specify a sort order
sort: [created_at
, desc
]
Pagination
config
:
list:
max_per_page
: 5
Joint on DB
what if I want the article titles in comments list?
config
:
list:
peer_method
: doSelectJoinBlogArticle
Form Validation
Done Automatically!
Credentials
config
:
# The id column is displayed only for users with the admin credential
list:
title
: List of Articles
display
: [id
, =title
, content
, nb_comments
]
fields
:
id
: { credentials
: [admin
] }
# The addcomment interaction is restricted to the users with the admin credential
actions
:
addcomment
: { credentials
: [admin
] }
filter:
display: [article_id, author, created_at]
Partial Filter
// Define the partial, in templates/_state.php
<?php echo $form[$name]->render($attributes->getRawValue()) ?>
// Add the partial filter in the filter list, in config/generator.yml
config:
filter: [date, _state]
config
:
list:
title
: List of Articles
object_actions
:
_edit
: ~
_delete
: ~
batch_actions
:
_delete
: ~
actions
:
_new
: ~
edit
:
title
: Body of article
%%title
%%
actions
:
_delete
: ~
_list
: ~
_save
: ~
_save_and_add
: ~
Your own action
list:
title
: List of Articles
object_actions
:
_edit
: -
_delete
: -
addcomment
: { label
: Add a comment
, action
: addComment
}
public function executeAddComment($request)
{
$comment = new BlogComment();
$comment->setArticleId($request->getParameter('id'));
$comment->save();
$this->redirect('comments_edit', $comment);
}
Custom Stylesheet
class: sfPropelGenerator
param:
model_class: BlogArticle
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: article
with_propel_route: 1
css: mystylesheet
Custom header/footer
_list_header.php
_list_footer.php
_form_header.php
_form_footer.php
Customizing the Theme
Final Project!